package com.yanchangapp.ui.activity.base;

import android.os.Bundle;
import android.support.v7.widget.AppCompatEditText;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.zxl.baselib.baseapp.AppManager;
import com.zxl.baselib.bean.response.BaseResponse;
import com.zxl.baselib.ui.base.BaseActivity;
import com.zxl.baselib.ui.base.BaseObserver;
import com.zxl.baselib.ui.base.BasePresenter;
import com.zxl.baselib.ui.base.ExceptionHandle;
import com.yanchangapp.R;
import com.yanchangapp.base.HeadApiRetrofit;
import com.yanchangapp.commom.AppConstant;
import com.yanchangapp.model.cache.UserCache;
import com.zxl.baselib.util.CheckUtils;
import com.zxl.baselib.util.Md5Utils;
import com.zxl.baselib.util.NetUtils;
import com.zxl.baselib.util.ui.UIUtils;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observable;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class ChangePwdActivity extends BaseActivity {
    @BindView(R.id.ivToolbarNavigation)
    ImageView ivToolbarNavigation;
    @BindView(R.id.vToolbarDivision)
    View vToolbarDivision;
    @BindView(R.id.tvToolbarTitle)
    TextView tvToolbarTitle;
    @BindView(R.id.etOldPwd)
    AppCompatEditText etOldPwd;
    @BindView(R.id.etNewPwd)
    AppCompatEditText etNewPwd;
    @BindView(R.id.etSecondPwd)
    AppCompatEditText etSecondPwd;
    @BindView(R.id.tvForgetPwd)
    TextView tvForgetPwd;
    @BindView(R.id.btnLogin)
    Button btnLogin;

    @Override
    protected void init() {

    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_change_pwd;
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        tvToolbarTitle.setText("修改密码");
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initListener() {

    }


    @OnClick({R.id.ivToolbarNavigation, R.id.tvForgetPwd, R.id.btnLogin})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ivToolbarNavigation:
                finish();
                break;
            case R.id.tvForgetPwd:
                jumpToActivity(ForgetPwdActivity.class);
                break;
            case R.id.btnLogin:
                doChangePwd();
                break;
        }
    }

    // 执行修改密码的逻辑
    public void  doChangePwd(){
        String oldPwd = etOldPwd.getText().toString();
        String newPwd = etNewPwd.getText().toString();
        String secondPwd = etSecondPwd.getText().toString();
        if (TextUtils.isEmpty(oldPwd)){
            UIUtils.showToast("请输入旧密码");
            return;
        }


        if (!CheckUtils.isPassword(oldPwd)){
            UIUtils.showToast("旧密码必须为6-16位数字或字母组合");
            return;
        }

        if (TextUtils.isEmpty(newPwd)){
            UIUtils.showToast("请输入新密码");
            return;
        }

        if (!CheckUtils.isPassword(newPwd)){
            UIUtils.showToast("新密码必须为6-16位数字或字母组合");
            return;
        }

        if (TextUtils.isEmpty(secondPwd)){
            UIUtils.showToast("请输入确认密码");
            return;
        }

        if (!CheckUtils.isPassword(secondPwd)){
            UIUtils.showToast("确认密码必须为6-16位数字或字母组合");
            return;
        }

        if (!newPwd.equals(secondPwd)){
            UIUtils.showToast("请保持密码一致");
            return;
        }

        if (!oldPwd.equals(UserCache.getPassword())){
            UIUtils.showToast("旧密码错误");
            return;
        }

        if (!NetUtils.isConnectedAndToast(this)){
            return;
        }

        HeadApiRetrofit.getInstance().updatePwd(UserCache.getID(), Md5Utils.md5(newPwd))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponse>() {
                    @Override
                    public void onNext(BaseResponse baseResponse) {
                        if (baseResponse.getCode() == AppConstant.REQUEST_SUCCESS){
                            UIUtils.showToast("修改密码成功");
                            doLogout();
                        }else {
                            UIUtils.showToast(baseResponse.getMsg());
                        }
                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseException exception) {
                        UIUtils.showToast("请求失败");
                    }
                });

    }

    private void doLogout(){
        Observable.create((ObservableOnSubscribe<Boolean>) emitter -> {
            UserCache.clear();
            emitter.onNext(true);
            emitter.onComplete();
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<Boolean>() {
                    @Override
                    public void onNext(Boolean aBoolean) {
                        if (aBoolean){
                            AppManager.getAppManager().finishAllActivity();
                            jumpToActivityAndClearTask(LoginActivity.class);
                        }
                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseException exception) {

                    }
                });

    }
}
